//
//  FPProductModel.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/4/24.
//

#import "FPProductModel.h"

@implementation FPProductModel

+ (void)getProductsWithType:(FPProductPostType)type success:(void (^)(NSInteger, NSString * _Nonnull, NSArray * _Nonnull))success failure:(void (^)(NSError * _Nonnull))failure {
    
    NSMutableDictionary *param = APIClient.creatAPIDictionary;
    
    if (type == FPProductPostTypeAll) {
            
        [param setObject:@"0" forKey:@"type"];
    }
    else if (type == FPProductPostType1) {
        
        [param setObject:@"1" forKey:@"type"];
    }
    else if (type == FPProductPostType2) {
        
        [param setObject:@"2" forKey:@"type"];
    }
    

    
    [[APIClient sharedInstance] requestWithURLString:[MAIN_URL stringByAppendingString:PRODUCTS_URL] parameters:param type:HttpRequestTypePost success:^(id responseObject, NSString *msg, NSInteger code) {
        
        NSError *error;
        
        id object = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        
        NSArray *list = [FPProductModel arrayOfModelsFromDictionaries:[object objectForKey:@"data"] error:&error];
        
        NSInteger httpCode = [[object objectForKey:@"code"] integerValue];
        
        NSString *message = [object objectForKey:@"msg"];
        
        if (success) {
            
            success(httpCode, message, list);
        }
        
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)clickProduct:(FPProductModel *)model success:(void (^)(BOOL isSuccess , NSString *URLString))success failure:(void (^)(NSError *error))failure {
    
    NSMutableDictionary *param = APIClient.creatAPIDictionary;
    
    NSString *number = [FPUserModel AES256Encrypt:FPUserInfoModel.sharedUserInfo.phone];
    [param setObject:number forKey:@"customerPhone"];
    
    [param setObject:model.advertId forKey:@"advertId"];
    [param setObject:model.annualInterestRate forKey:@"annualInterestRate"];
    [param setObject:model.companyId forKey:@"companyId"];
    [param setObject:model.companyName forKey:@"companyName"];
    [param setObject:model.h5Url forKey:@"h5Url"];
    
    [param setObject:model.loanAmount forKey:@"loanAmount"];
    [param setObject:model.productLogo forKey:@"productLogo"];
    [param setObject:model.productName forKey:@"productName"];
    [param setObject:model.sellingPoint forKey:@"sellingPoint"];
    [param setObject:model.sourceType forKey:@"sourceType"];
    
    [param setObject:model.price forKey:@"price"];
    [param setObject:model.requestId forKey:@"requestId"];
    
    
//    [param setObject:model.click forKey:@"click"];
    if (model.click.integerValue == 0) {
        
        [param setObject:@(NO) forKey:@"click"];
    }
    else {
        
        [param setObject:@(YES) forKey:@"click"];
    }
    
    [param setObject:model.customerId forKey:@"customerId"];
    [param setObject:model.customerSourceType forKey:@"customerSourceType"];
    [param setObject:model.sourceOfClue forKey:@"sourceOfClue"];
    
    [[APIClient sharedInstance] requestWithURLString:[MAIN_URL stringByAppendingString:PRODUCT_CLICK_URL] parameters:param type:HttpRequestTypePost success:^(id responseObject, NSString *msg, NSInteger code) {
        
        NSError *error;
        
        id object = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        
        BOOL ret = NO;
        NSString *URLString;
        
        if ([object isKindOfClass:NSDictionary.class]) {
            
            id obj = [object objectForKey:@"data"];
            
            if ([obj isKindOfClass:NSDictionary.class]) {
                
                ret = [[(NSDictionary *)obj objectForKey:@"succeed"] boolValue];
                URLString = [obj objectForKey:@"url"];
            }
        }
        
        if (success) {
            
            success(ret , URLString);
        }
        
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}



@end
